<template>
  <div>
    <el-form ref="deptForm" :model="form" label-width="120px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="addList">确定</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeeSimple } from '../../api/employee'
import { addDepartments, updateDepartments } from '../../api/departments'
export default {
  name: '',
  props: {
    pid: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean
    },
    // eslint-disable-next-line vue/require-default-prop
    row: {
      type: Object
    },
    isEdit: {
      type: Boolean
    }
  },
  data() {
    return {
      form: {
        code: '',
        manager: '',
        introduce: '',
        name: '',
        pid: ''
      },
      managerList: [],
      rules: {
        code: [{ required: true, trigger: 'blur', message: '请输入部门编码' }],
        name: [{ required: true, trigger: 'blur', message: '请输入部门名称' }],
        introduce: [
          { required: true, trigger: 'blur', message: '请输入部门介绍' }
        ]
      }
    }
  },
  computed: {},
  watch: {
    isEdit: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.form = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true
    }
  },
  created() {
    this.getEmp()
    // this.doadd()
  },
  methods: {
    async getEmp() {
      const { data } = await getEmployeeSimple()
      console.log(data, 55522)
      this.managerList = data
    },
    addList() {
      this.$refs.deptForm.validate((result) => {
        if (result) {
          this.isEdit ? this.doEdit() : this.doadd()
        }
      })
    },
    async doadd() {
      this.form.pid = this.pid
      const res = await addDepartments(this.form)
      if (res.code === 10000) {
        this.$emit('success')
        this.close()
      }
    },
    async doEdit() {
      const res = await updateDepartments(this.form)
      if (res.code === 10000) {
        this.$emit('success')
        console.log(res)
      }
    },
    cancel() {
      this.$emit('update:visible', false)
    },
    close() {
      this.form = {
        code: '',
        manager: '',
        introduce: '',
        name: '',
        pid: ''
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
